//
//  BSLNavigator.m
//  Chamleon-template
//
//  Created by YLW on 15/10/23.
//
//

#import "BSLNavigator.h"
#import "NavigationViewController.h"
#import "UIView+Extension.h"
#import "UIColor+Extension.h"

@implementation NavigatorInfo

- (instancetype)init
{
    if (self = [super init])
    {
        self.fullscreen = NO;
        self.style = @"UIStatusBarStyleBlackOpaque";
        self.rgb = @"#4687C7";
    }
    return  self;
}

- (void)setRgb:(NSString *)rgb
{
    if (rgb.length == 0 )
    {
        return;
    }
    _rgb = [rgb copy];
}
- (void)setStyle:(NSString *)style
{
    if (style.length < 23)
    {
        return;
    }
    _style = [style copy];
}

- (BOOL)isIsVersion7
{
    return [[UIDevice currentDevice].systemVersion floatValue] >= 7.0;
}

@end


@implementation BSLNavigator


#pragma mark - 设置cookie
- (void)setCookie:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    NSString *url = [command argumentAtIndex:0 withDefault:nil];
    NSString *value = [command argumentAtIndex:1 withDefault:nil];
    
    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
    [cookieProperties setObject:@"username" forKey:NSHTTPCookieName];
    [cookieProperties setObject:value == nil ? @"rainbird" : value forKey:NSHTTPCookieValue];
    [cookieProperties setObject:url == nil ? @"baidu.com" : url forKey:NSHTTPCookieDomain];
    [cookieProperties setObject:url == nil ? @"baidu.com" : url forKey:NSHTTPCookieOriginURL];
    [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
    [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
    
    NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
    [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    
}

#pragma mark - 获取cookie
- (void)getCookie:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    self.navigatorInfo.url = [command argumentAtIndex:0 withDefault:nil];
    if (self.navigatorInfo.url.length <= 0 || self.navigatorInfo.url == nil)
    {
        return;
    }
    
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        NSLog(@"%@",cookie.domain);
        if ([cookie.domain isEqualToString:self.navigatorInfo.url])
        {
            NSString *cookieStr = [cookie description];
            cookieStr = [cookieStr stringByReplacingOccurrencesOfString:@"<" withString:@""];
            cookieStr = [cookieStr stringByReplacingOccurrencesOfString:@">" withString:@""];            NSLog(@"%@",cookieStr);
            CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:cookieStr];
            [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
        }
    }

}

#pragma  mark - 删除所有cookie
- (void)removeAllCookie:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        [cookieJar deleteCookie:cookie];
    }
}

#pragma mark - 删除应用对应的cookie
- (void)removeCookie:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    self.navigatorInfo.url = [command argumentAtIndex:0 withDefault:nil];
    if (self.navigatorInfo.url.length <= 0 || self.navigatorInfo.url == nil)
    {
        return;
    }
    
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        NSLog(@"%@",cookie.domain);
        if ([cookie.domain isEqualToString:self.navigatorInfo.url])
        {
            [cookieJar deleteCookie:cookie];
        }
    }

    
}

- (void)removeSessionCookie:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (NSHTTPCookie *cookie in [cookieJar cookies]) {
        [cookieJar deleteCookie:cookie];
    }

}
#pragma mark - 设置是否全屏
- (void)setFullscreen:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    
    int  fullscreen =  [[command argumentAtIndex:0 withDefault:nil] intValue];
    
    BOOL isfullscreen = fullscreen ? YES : NO;
    
    if (self.navigatorInfo.fullscreen == isfullscreen) {
        return;
    }
    
    self.navigatorInfo.fullscreen = isfullscreen;
    NavigationViewController *navigationVC =[NavigationViewController sharedInstance];
   
    [[UIApplication sharedApplication] setStatusBarHidden:self.navigatorInfo.fullscreen withAnimation:UIStatusBarAnimationSlide];
    [UIView animateWithDuration:0.3 animations:^{
        navigationVC.bgView.y = self.navigatorInfo.fullscreen ? -20 : 0 ;
            if (!self.navigatorInfo.fullscreen)
            {
            navigationVC.bgView.hidden = self.navigatorInfo.fullscreen;
            }
        } completion:^(BOOL finished) {
            navigationVC.bgView.hidden = self.navigatorInfo.fullscreen;
        }];

}

#pragma mark - 设置状态栏背景颜色
- (void)setStatusBarBackground:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    NSString *rgb = [command argumentAtIndex:0 withDefault:nil];
    
    if ([self.navigatorInfo.rgb isEqualToString:rgb]) {
        return;
    }
    
    self.navigatorInfo.rgb = rgb;
    
    [NavigationViewController sharedInstance].bgView.backgroundColor = [UIColor hexStringToColor:self.navigatorInfo.rgb];
    
}

#pragma mark - 设置状态栏样式
- (void)setStatusBarStyle:(CDVInvokedUrlCommand *)command
{
    
    self.callbackID = command.callbackId;
    
    NSString *style = [command argumentAtIndex:0 withDefault:nil];
    if ([self.navigatorInfo.style isEqualToString:style]) {
        return;
    }
    
    self.navigatorInfo.style = style;
    
    [[UIApplication sharedApplication] setStatusBarStyle:[self getStatusBarStyle]animated:YES];
    
}

#pragma mark - 获取是否是全屏显示
- (void)isFullscreen:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:self.navigatorInfo.fullscreen];
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
    
}

#pragma mark - 获取状态栏样式
- (void)getStatusBarStyle:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:self.navigatorInfo.style];
    
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
    
}

#pragma mark - 获取状态栏颜色
- (void)getStatusBarBackground:(CDVInvokedUrlCommand *)command
{
    self.callbackID = command.callbackId;
    
    CDVPluginResult *result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:self.navigatorInfo.rgb];
    [self.commandDelegate sendPluginResult:result callbackId:self.callbackID];
}

- (UIStatusBarStyle)getStatusBarStyle{
    if ([self.navigatorInfo.style  isEqualToString:@"UIStatusBarStyleBlackOpaque"]) {
        return self.navigatorInfo.isVersion7 ?  UIStatusBarStyleLightContent : UIStatusBarStyleBlackOpaque;
    }
    else if([self.navigatorInfo.style isEqualToString:@"UIStatusBarStyleBlackTranslucent"])
    {
        return self.navigatorInfo.isVersion7 ? UIStatusBarStyleLightContent : UIStatusBarStyleBlackTranslucent;
    }
    else{
        return UIStatusBarStyleDefault;
    }
}


#pragma mark - 懒加载
- (NavigatorInfo *)navigatorInfo
{
    if (_navigatorInfo == nil)
    {
        _navigatorInfo = [[NavigatorInfo alloc] init];
    }
    return _navigatorInfo;
}

@end
